///负责处理http 服务请求 模块

const http = require("http");
const express = require("express");
const path = require("path");
const url = require("url");
let Nconsole = require("@michaelray/console-color");
// cookies处理模块
var cookieParser = require("cookie-parser");

//导入自定义中间件- 处理uuid
// const createUUID = require("./createUID");

//导入路由模块
const routerInit = require("../router");

// session操作模块导入
// const sessionCheck = require("../controller/sessionCheck");

// 启动服务
const app = express();
const server = http.createServer(app);

//配置静态资源目录
// app.use(express.static(path.join(process.cwd(), "src/static")));
app.use(express.static(path.join(process.cwd(), "dist/static")));

// 处理请求数据类型
app.use(cookieParser());
const bodyParser = require("body-parser");
app.use(bodyParser.json()); //数据JSON类型
app.use(
    bodyParser.urlencoded({
        extended: false,
    })
); //解析post请求数据
app.all("*", function (req, res, next) {
    // let origin = req.headers.origin;
    res.setHeader("Access-Control-Allow-Credentials", true);
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type"); //预检测设置，自动识别全部类型
    next();
});

//初始化路由
routerInit(app);
//session操作
// sessionCheck(app);
function Thistime() {
    //当前日期
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var mytime = date.toLocaleTimeString(); //获取当前时间
    // myDate.toLocaleString( );
    var time = year + "-" + month + "-" + day + " " + mytime;
    return time;
}
app.use(cookieParser("13128755602"));

//配置一个自定义express中间件 ：用于检测用户是否具有uuid cookie ，如果没有，则创建一个uuid
// app.use(createUUID());
const config = require("../config/config");
// console.log(`您的web服务已经运行在 http://` + config.host + `:` + config.port);
Nconsole.info(`您的web服务已经运行在 http://` + config.host + `:` + config.port);
// Nconsole.debug("debug");
// Nconsole.warn("warn");
// Nconsole.error("error");
module.exports = server;

// ======= 原生中使用 session
const session = require("express-session");
// const uuid = require("uuid/v1"); // 生成随字符串
const querystring = require("querystring");
var cookie = require("cookie-parser");
// var session = require("express-session");
// seetions 初始化
app.use(
    session({
        secret: "keyboard cat", //密钥
        resave: true, //强制保存session
        saveUninitialized: true, //是否保存初始化的session
        cookie: {
            maxAge: 5 * 60 * 1000,
            secure: false,
        },
        name: "testapp", //这里的name指的是cookie的name，默认cookie的name是：connect.sid
    })
);